<?php

namespace app\keeper\controller;

use app\common\model\mysql\WarehouseSpaceProduct;
use app\common\model\mysql\Product as productModel;
class Product extends AuthBase
{
    /**
     * 商品列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function productList(){
        $page = $this->request->param('pageNum', 1);//当前页
        $limit = $this->request->param('pageSize', 10);//每页显示条数
        $product_name = $this->request->param('product_name');
        $typeenum = $this->request->param('typeenum');
        if(empty($page) || empty($limit)){
            return show(config("status.error"),"缺少分页参数");
        }
        $where = [
            ['company_id','=',$this->companyId],
            ['shop_id','=',$this->shopId],
            ['isdel','=','0']
        ];
        if($typeenum){
            $where[] = ['typeenum','=',$typeenum];
        }
        if($product_name){
            $where[] = ['product_name','like','%'.trim($product_name).'%'];
        }
        $productData = (new productModel())->getNormalProductList($where,$limit,$page,'product_id,product_name,tcount,price_unit,typeenum');
        $productData = $productData['data'];
        foreach ($productData as $key=>$value){
            $productId = $value['product_id'];
            if($typeenum == 1){
                $productWhere = [
                    ['parentid','=',$value['product_id']],
                    ['typeenum','=',4],
                    ['isdel','=','0']
                ];
                $bucketData = (new productModel())->getProductFind($productWhere,'product_id');
                $productId = $bucketData['product_id'];
            }
            $warehouseSpaceProductWhere = [
                ['a.product_id','=',$productId],
                ['a.isdel','=','0']
            ];
            $productData[$key]['saveGoods'] = $orderProductNormalData[$key]['saveGoods'] = (new WarehouseSpaceProduct())->getNormalWarehouseSpaceProductList($warehouseSpaceProductWhere,'a.warehouse_space_product_id as id,d.product_id,d.product_name,a.warehouse_id,a.warehouse_space_id,b.warehouse_name,c.warehouse_space_name,a.tcount');
        }
        return show(config("status.success"),"商品列表",$productData);
    }
}
